package com.zxw.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxw.common.core.base.enums.EnumBeanNameEnum;
import com.zxw.common.web.util.BeanUtils;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.flow.domain.entity.FlowNodes;
import com.zxw.flow.domain.entity.FlowNodesRelation;
import com.zxw.flow.domain.entity.FlowType;
import com.zxw.flow.domain.vo.FlowNodesPageVO;
import com.zxw.flow.domain.vo.FlowNodesRelationPageVO;
import com.zxw.flow.domain.vo.FlowNodesRelationVO;
import com.zxw.flow.domain.vo.FlowNodesVO;
import com.zxw.flow.mapper.FlowNodesRelationMapper;
import com.zxw.flow.service.FlowNodesRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-07-04
 */
@Service
@RequiredArgsConstructor
public class FlowNodesRelationServiceImpl extends ServiceImpl<FlowNodesRelationMapper, FlowNodesRelation> implements FlowNodesRelationService {

    private final FlowNodesRelationMapper flowNodesRelationMapper;

    @Override
    public IPage<FlowNodesRelation> getPage(FlowNodesRelationPageVO pageVO) {
        Page<FlowNodesRelation> page = new Page<>(pageVO.getPageNum(), pageVO.getPageSize());
        LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<FlowNodes>()
                .eq(FlowNodes::getCompanyId, pageVO.getCompanyId())
                .eq(FlowNodes::getFlowBasicInfoId, pageVO.getFlowBasicInfoId())
                .eq(null != pageVO.getNodeName(), FlowNodes::getNodeName, pageVO.getNodeName())
                .eq(null != pageVO.getShape(), FlowNodes::getShape, pageVO.getShape());
        Page<FlowNodesRelation> pageData = flowNodesRelationMapper.selectPage(page, wrapper);
        return pageData;
    }

    @Override
    public FlowNodesRelationVO add(FlowNodesRelationVO flowNodesRelationVO) {
        flowNodesRelationMapper.insert(flowNodesRelationVO);
        return flowNodesRelationVO;
    }

    @Override
    public FlowNodesRelation getFlowNodesRelationById(Long id) {
        Long companyId = JwtUtils.getCompanyId();
        FlowNodesRelation flowType = flowNodesRelationMapper.selectOne(new LambdaQueryWrapper<FlowNodesRelation>()
                .eq(FlowNodesRelation::getId, id)
                .eq(FlowNodesRelation::getCompanyId, companyId)
        );
        return flowType;
    }

    @Override
    public void updateBase(FlowNodesRelationVO flowNodesRelationVO) {
        flowNodesRelationMapper.update(flowNodesRelationVO, new LambdaUpdateWrapper<FlowNodesRelation>()
                .eq(FlowNodesRelation::getId, flowNodesRelationVO.getId())
                .eq(FlowNodesRelation::getCompanyId, flowNodesRelationVO.getCompanyId())
        );
    }

    @Override
    public void delete(String flowNodesRelationIds) {
        Long companyId = JwtUtils.getCompanyId();
        flowNodesRelationMapper.delete(new LambdaUpdateWrapper<FlowNodesRelation>()
                .eq(FlowNodesRelation::getCompanyId, companyId)
                .in(FlowNodesRelation::getId, flowNodesRelationIds)
        );
    }
}
